<?php
// +----------------------------------------------------------------------
// | ITKEE [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.itkee.cn.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: SuperMan <superman@itkee.cn>
// +----------------------------------------------------------------------
// | 用户管理
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\model\SkillInfo;
use app\common\model\User as UserModel;
use app\common\controller\AdminBase;
use think\Cache;
use think\Config;
use think\Db;

/**
 * 用户管理
 * Class AdminUser
 * @package app\admin\controller
 */
class User extends AdminBase
{
    protected $user_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->user_model = new UserModel();
        $identity_list = Cache::get('identity_list_info');
        $this->assign('identity_list',$identity_list);
    }

    /**
     * 用户列表
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index($keyword = '', $page = 1)
    {
        $map = [];
        if ($keyword) {
            $map['u.u_email'] = ['like', "%{$keyword}%"];
        }
        $user_list = $this->user_model->alias('u')
            ->join('user uone', 'u.u_superior = uone.u_id', 'left')
            ->where($map)
            ->field('u.*, uone.u_email as uoneeamil')
            ->order('u.u_id DESC')
            ->paginate(15, false, ['page' => $page]);

        $allnum = Db::name('user')->count();
        $membernum = Db::name('user')->where('u_type', 1)->count();
        return $this->fetch('index', ['user_list' => $user_list, 'keyword' => $keyword, 'allnum' => $allnum, 'membernum' => $membernum]);
    }
    /*
     * 添加用户
     */
    public function add()
    {
        return $this->fetch('add');
    }
    /**
    * 保存用户
    */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'User');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $data['u_password'] = md5(Config::get('salt').$data['u_password']);
                $data['u_type'] = 2;
                unset($data['confirm_password']);
                if (\app\common\model\User::addUser($data)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }
    /**
     * 用户详情
     */
    public function detail($id)
    {
        $detail = $this->user_model->alias('u')
            ->join('user uone', 'u.u_superior = uone.u_id', 'left')
            ->where('u.u_id', $id)
            ->field('u.*, uone.u_email as uoneeamil')
            ->find();
        return $this->fetch('detail', ['detail' => $detail]);
    }
    /**
     * 禁用启用用户
     * @param $id
     */
    public function edit($id, $u_status)
    {
        if ($u_status == 1) {
            $u_status = 2;
        } else if ($u_status == 2) {
            $u_status = 1;
        } else {
            $this->error('修改失败');
        }
        $status = $this->user_model->where('u_id', $id)->update(['u_status' => $u_status]);
        if ($status) {
            $this->success('修改成功');
        } else {
            $this->error('修改失败');
        }
    }


    /**
     * 证券收益
     */
    public function securitiesincome($id, $email)
    {
        $detail = \app\common\model\User::userSecuritiesIncome($id);
        return $this->fetch('securitiesincome', ['detail' => $detail, 'email' =>$email]);
    }

    /**
     * 推广用户
     */
    public function extensionuser()
    {
        $list = \app\common\model\User::offlinelist(input()['id']);
        return $this->fetch('extensionuser', ['list' => $list['list'],'lista' => $list['lista'], 'listb' => $list['listb']]);

    }

    /**
     * 重置密码
     */
    public function updatepassword($id)
    {
        $status = \app\common\model\User::resetpassword($id);
        if ($status) {
            $this->success('密码重置成功，新密码为‘123456’');
        } else {
            $this->error('重置密码失败，请稍后重试');
        }

    }

    /**
     * 账号技能信息
     */
    public function skillinfo($id)
    {
        $detail = SkillInfo::skillInfoForUid($id);
        return $this->fetch('skillinfo', ['detail' => $detail]);
    }

//
//
//    /**
//     * 添加用户
//     * @return mixed
//     */
//    public function add()
//    {
//        return $this->fetch();
//    }
//
//    /**
//     * 保存用户
//     */
//    public function save()
//    {
//        if ($this->request->isPost()) {
//            $data            = $this->request->post();
//            $validate_result = $this->validate($data, 'User');
//
//            if ($validate_result !== true) {
//                $this->error($validate_result);
//            } else {
//                $data['password'] = md5(Config::get('salt').$data['password']);
//                if ($this->user_model->allowField(true)->save($data)) {
//                    $this->success('保存成功');
//                } else {
//                    $this->error('保存失败');
//                }
//            }
//        }
//    }


    /**
     * 更新用户
     * @param $id
     */
    public function update()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $res = Db::name('user')->where('u_id', $data['u_id'])->update([
                'u_alipay'          =>  $data['u_alipay'],
                'u_bank_type'       =>  $data['u_bank_type'],
                'u_bank_card_code'  =>  $data['u_bank_card_code'],
                'u_name_bank'       =>  $data['u_name_bank'],
                'u_bank_phone'      =>  $data['u_bank_phone'],
                'u_dollar_balance'  =>  $data['u_dollar_balance'],
                'u_update_time'     => time()
            ]);
            if ($res) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }
        }
    }


}